#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>

#include "mosquitto.h"
#include "connect.h"
#include "mosq.h"
#include "cJSON.h"
#include "struct.h"
#include "mosqopt.h"


static int g_stop = 0;

void sig_handle(int signum)
{
	if(SIGUSR1 == signum)
	{
		g_stop = 1;
	}
}


int main(int argc,char **argv)
{
	struct mosquitto 		*mosq;
	char                  		buf[1024];
	int				rv=-1;
	int				qos=0;
	mosquitto_t			mqtt;



	if(argc<3)
	{
		printf("Usage: %s -c filename.json\n",argv[0]);

		return -1;
	}

	memset(&mqtt,0,sizeof(mosquitto_t));
	rv=mosq_argc(argc,argv,&mqtt);

	if(rv!=0)
	{
		printf("Read json file failure!");
		return -1;
	}


	//	memset(&mqtt,0,sizeof(mosquitto_t));

	signal(SIGUSR1,sig_handle);
#if 0

	/*aliyun*/
	mqtt.clientId= "ihy7ytSUVSx.AGT-DS18B20-01|securemode=2,signmethod=hmacsha256,timestamp=1681889819251|";
	mqtt.username = "AGT-DS18B20-01&ihy7ytSUVSx"  ;
	mqtt.hostname="iot-06z00bdr7q1tel1.mqtt.iothub.aliyuncs.com"  ;
	mqtt.passwd = "136e953f5cdf89637a39247aecc52c1114c1e525dff2d61b381ae6b86d42a054"  ;
	mqtt.port = 1883  ;
	mqtt.topic = "/sys/ihy7ytSUVSx/AGT-DS18B20-01/thing/event/property/post";
	//	rv=connect_to_broker(mosq,mosquitto->clientId,mosquitto->username,mosquitto->hostname,mosquitto->passwd,mosquitto->port,mosquitto->topic);
	/*iotcloud.tencent*/
	mqtt.clientId="B4GBDMC0Q3rpi_001";
	mqtt.username = "B4GBDMC0Q3rpi_001;12010126;93edf;1690041600";
	mqtt.hostname = "B4GBDMC0Q3.iotcloud.tencentdevices.com";
	mqtt.passwd = "d0147d3b67f16414ae98607b5fa6261d20d805a511c147240882f1f101983a20;hmacsha256";
	mqtt.port = 1883;
	mqtt.topic="$shadow/operation/B4GBDMC0Q3/rpi_001";
#endif


	rv=connect_to_broker(mosq,&mqtt);
	if(rv<0)
	{
		printf("Connect to broker failure!\n");
		return -1;
	}

	memset(buf,0,sizeof(buf));

	printf("Publish success!\n");


	return 0;

}
